//
//  KKArticalVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/23.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKArticalVC.h"
#import "KKArticalCell.h"
#import "KKPublishArticleVC.h"
#import "KKComHttp.h"
#import "KKArticleDetailVC.h"
#import "KKWebViewVC.h"
#import "KKMyHttp.h"

@interface KKArticalVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) int page;
@property (strong, nonatomic) NSMutableArray *articleDatas;
@end

@implementation KKArticalVC
-(NSMutableArray *)articleDatas{
    if(!_articleDatas){
        _articleDatas=[NSMutableArray array];
    }
    return _articleDatas;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.page=1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getArticleList)];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKArticalCell" bundle:nil] forCellReuseIdentifier:@"KKArticalCell"];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadMoreData];
    }];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKArticalCell *article=(KKArticalCell *)cell;
    [article initData:self.articleDatas[indexPath.row]];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.articleDatas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKArticalCell"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKArticleListM *article= self.articleDatas[indexPath.row];
    if(article.contentimg!=nil&&article.contentimg.count>0){
        return 151;
    }else{
        return 101;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKArticleListM *data= self.articleDatas[indexPath.row];
    KKArticleDetailVC *detail=[[KKArticleDetailVC alloc] init];
    detail.docId=data.articleid;
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark 按钮--官网消息与广告
- (IBAction)msgAndAdClick:(id)sender {
//    KKWebViewVC *webView=[[KKWebViewVC alloc] init];
//    webView.navTitle=@"官网消息与广告";
//    webView.url=@"http://www.baidu.com";
//    [self.navigationController pushViewController:webView animated:YES];
}

- (IBAction)publishArticleClick:(id)sender {
    [KKMyHttp userCheck:^(id data) {
        KKPublishArticleVC *publish=[[KKPublishArticleVC alloc] init];
        publish.refreshListBlock=^(){
            [self.tableView.mj_header beginRefreshing];
        };
        [self.navigationController pushViewController:publish animated:YES];
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark 接口--文章列表
-(void)getArticleList{
    [KKComHttp getArticleList:@"1" success:^(id data) {
        self.page=1;
        [self.articleDatas removeAllObjects];
        [self.articleDatas addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
-(void)loadMoreData{
    [KKComHttp getArticleList:[NSString stringWithFormat:@"%d",self.page] success:^(id data) {
        [self.articleDatas addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

@end
